<template>
	<view class="goods">
		<view v-for="item in goodsList" :key="item.goods_id" @click='gotoDetail(item)'>
			<my-goods :goodsInfo='item'></my-goods>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj: {
					// 查询关键词
					query: '',
					// 商品分类Id
					cid: '',
					// 页码值
					pagenum: 1,
					// 每页显示多少条数据
					pagesize: 10
				},
				goodsList: [],
				total: 0,
				isLoading: false
			};
		},
		onLoad(options) {
			console.log(options);
			this.queryObj.cid = options.cid || ''
			this.queryObj.query = options.query || ''
			this.getGoodslist()
		},
		onReachBottom() {
			console.log(6666);
			if (this.goodsList.length === this.total) {
				console.log('够了够了 ');
				return uni.showToast({
					title: '人家也是有底线的哦！',
					duration: 1500,
					icon: 'none',
				})
			}
			this.queryObj.pagenum++
			this.getGoodslist()
		},
		onPullDownRefresh() {
			console.log('下拉刷新了');
			this.goodsList = []
			this.queryObj.pagenum = 1
			this.getGoodslist(() => {
				uni.stopPullDownRefresh()
			})
		},
		methods: {
			async getGoodslist(fn) {
				if (this.isLoading) {
					return
				}
				this.isLoading = true
				const {
					data
				} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				this.isLoading = false
				fn && fn()
				console.log(data);
				if (data.meta.status !== 200) {
					return uni.showToast({
						title: '数据请求失败！',
						duration: 1500,
						icon: 'none',
					})
				}
				this.goodsList = [...this.goodsList, ...data.message.goods]
				this.total = data.message.total
			},
			gotoDetail(item) {
				console.log(111);
				uni.navigateTo({
					url: '/subpkg/goods-detail/goods-detail?goods_id=' + item.goods_id
				})
			}
		}
	}
</script>

<style lang="scss">

</style>
